library(MDEI)
options(device = "quartz")

x <- (1:100) / 101

# Make bases----
b1 <- MDEI:::bs.me(x, "X")
b1 <- cbind(b1, MDEI:::bs.me(-x, "X"))
db1 <- MDEI:::dbs.me(x)
db1 <- cbind(db1,-MDEI:::dbs.me(-x))


keeps <- MDEI:::checkcor(b1, .99)
b1 <- b1[, which(keeps == 1)]
db1 <- db1[, which(keeps == 1)]

scale2 <- function(z)
  (z - min(z)) / (diff(range(z)))

# Figure for bases----

pdf("Figure1_basesexample.pdf", h = 4, w = 8)
par(mfrow = c(1, 2), mar = c(3, 3, 2, 1))

plot(
  x,
  b1[, 1],
  type = "n",
  ylim = c(0, 1),
  xlab = "",
  ylab = ""
)
for (i in 1:ncol(b1))
  lines(x, scale2(b1[, i]))
mtext(side = 1, text = "Covariate, x", line = 2)
mtext(
  side = 3,
  text = expression(paste("Basis Functions of ", theta, ", ", phi(x))),
  line = .2,
  cex = 1.25
)
mtext(side = 2,
      text = expression(paste("Value of ", phi(x))),
      line = 2)


plot(
  x,
  b1[, 1],
  type = "n",
  ylim = c(0, 1),
  xlab = "",
  ylab = ""
)
for (i in 1:ncol(b1))
  lines(x, scale2(db1[, i]))
mtext(side = 1, text = "Covariate, x", line = 2)
mtext(
  side = 3,
  text = expression(paste("Basis Function of ", tau, ", ", dot(phi)(x))),
  line = .2,
  cex = 1.25
)
mtext(side = 2,
      text = expression(paste("Value of ", dot(phi)(x))),
      line = 2)
dev.off()
